<?php namespace Manage;

use Illuminate\Database\Eloquent\Model;

class Base extends Model {

	public $msg;

	public function __construct()
	{
		//构造消息标示
	    $this->msg = $this;
	}

	/**
	 * 错误消息返回
	 * @param string $mark 错误标示
	 * @param string $msg  消息
	 */
	static public function setErrorMsg($mark, $msg = '')
	{
	    return array(0, $mark, $msg);
	}

	/**
	 * 成功消息返回
	 * @param string $msg 消息
	 */
	static public function setSuccessMsg($msg = '')
	{
		return array(1, '', $msg);
	}

	/**
	 * 用户登录验证
	 * @return [type] [description]
	 */
	static public function validate()
	{

		$cookie = !empty($_COOKIE['user'])?$_COOKIE['user']:false;

		switch (true) 
		{
		
			case \Session::has('user'):
				return true;
			
			case $cookie:
				\Session::put('user', $cookie);
				return true;

			default:
				return false;

		}
	}
	
	/**
	 * 获取代理商ID
	 * @return [type] [description]
	 */
	static function getAgentId()
	{
		return \Session::get('id');
	}

	/**
	 * 用户数据存储/清理
	 * @param  int $opt 操作类型(1.存储 0.清理)
	 * @return [type]      [description]
	 */
	static public function rememberOpt($opt, $user='', $pwd='')
	{
		if( $opt == 1)
		{
			\Session::put('u', \Crypt::encrypt($user));
			\Session::put('p', \Crypt::encrypt($pwd));
		}
		elseif( $opt == 0 )
		{
			if( \Session::has('u') )
			{
				\Session::forget('u');
			}
			if( \Session::has('p') )
			{
				\Session::forget('p');
			}
		}
	}


	/**
	 * 用户数据获取
	 * @return [type]      [description]
	 */
	static public function rememberGet()
	{
		if( \Session::has('u') && \Session::has('p') )
		{
			return array(

						'usr' => \Crypt::decrypt(\Session::get('u')),
						'pwd' => \Crypt::decrypt(\Session::get('p'))

					);
		}

		return false;
	}


	/**
	 * 数据合并传输
	 * @param  string $name 传输名
	 * @param  array|string|int $data 传输值
	 * @param  [type] $obj  处理对象
	 * @return [type]       [description]
	 */
	static public function dPush($name, $data, $obj)
	{
		$obj->data[$name] = $data;
	}


  	/**
  	 * 数据请求
  	 * @param  [type] $url  请求地址
  	 * @param  [type] $post 数据集
  	 * @param  [type] $headers 头部信息
  	 * @return [type]       [description]
  	 */
	static public function socketPost($url, $post, $headers = '') 
	{  

		if( !empty($headers) )
		{
			$headers .= 'U-ApiKey: '.$headers;
		}

	    $urls = parse_url($url);
	    if (!isset($urls['port'])) 
	    {
	        $urls['port'] = 80;
	    }

	    $fp = fsockopen($urls['host'], $urls['port'], $errno, $errstr);
	    if (!$fp) 
	    {
	        echo "$errno, $errstr";
	        exit();
	    }

	    $post = http_build_query($post);
	    $length = strlen($post);
	    $header = <<<HEADER
POST {$urls['path']} HTTP/1.1  
Host: {$urls['host']}  
Content-Type: application/x-www-form-urlencoded  
Content-Length: {$length}  
Connection: close  
{$headers} 

{$post} 
HEADER;
  
	    fwrite($fp, $header);  
	    $result = '';  
	    while (!feof($fp)) 
	    {  
	        // receive the results of the request  
	        $result .= fread($fp, 512);  
	    }  
	    $result = explode("\r\n\r\n", $result, 2);  
	    return $result[1];  
 
	}

}
